<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>debounce</title>
</head>

<body>
    <button id="btn">提交</button>

    <script>

        // 某一个时间内，只能触发一次函数，使用 setTimeout 解决
        const btn = document.getElementById('btn')
        btn.addEventListener('click', debounce(submit), false)

        function submit() {
            console.log(1);

        }

        function debounce(fn, time = 300) {
            let id = null
            return function () {
                if (id) clearTimeout(id)
                id = setTimeout(() => {
                    fn()
                }, time)


            }


        }



    </script>
</body>

</html>